﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KinectDAL;

namespace Kinect_Repository
{
    public class SettingsApplication
    {
        public SettingsApplication()
            : this(false)
        {

        }
        public SettingsApplication(bool load)
        {
            if (load)
                this.Load();
        }
        public const string NameConnectionDefault = "name=Kinect";
        private const string cUserName ="UserName";       
        private const string cApplicationGuid = "ApplicationGuid";
        private const string cTimeLimit = "TimeLimit";

        public string UserName { get; set; }
        public string ApplicationGuid { get; set; }
        private string sTimeLimit { get; set; }

        public int TimeLimitSeconds
        {
            get
            {
                int timelimit;
                if (int.TryParse(sTimeLimit, out timelimit))
                {
                    return timelimit;
                }
                return 60 * 3;//3 minutes
            }
            set
            {
                sTimeLimit = value.ToString();
            }
        }

        public bool IsUserRegistered
        {
            get
            {
                return !(string.IsNullOrWhiteSpace(UserName) || string.IsNullOrWhiteSpace(ApplicationGuid));
            }
        }

        public void Load()
        {
            using (Setting_List se = new Setting_List(NameConnectionDefault))
            {
                se.LoadFromDB.LoadAll1();
                foreach (var item in se)
                {
                    switch (item.NameSetting)
                    {
                        case cUserName:
                            UserName = item.ValueSetting;
                            break;
                        case cApplicationGuid:
                            ApplicationGuid = item.ValueSetting;
                            break;
                        case cTimeLimit:
                            sTimeLimit = item.ValueSetting;
                            break;
                        default:
                            //TODO: log
                            break;
                    }
                }
            }
        }
        public void DeleteAll()
        {
            using (Setting_List se = new Setting_List(NameConnectionDefault))
            {
                se.FastDeleteAll();
            }
        }
        public void Save()
        {
            DeleteAll();
            using (Setting_List se = new Setting_List(NameConnectionDefault))
            {                
                se.AddNew(cUserName, UserName);
                if (sTimeLimit == null)
                    sTimeLimit = TimeLimitSeconds.ToString();
                se.AddNew(cTimeLimit, sTimeLimit);
                se.AddNew(cApplicationGuid, ApplicationGuid);
                se.SaveNew();
            }
        }
    }
}
